package org.example.system.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.example.system.entity.AssignedEntity;
import org.example.system.service.AssignedService;
import org.example.system.vo.AssignedVo;
import org.example.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 排课控制层
 */
@RestController
@RequestMapping("/admin/assigned")
@Slf4j
public class AssignedController {

    @Autowired
    private AssignedService assignedService;

    /**
     * 按条件分页查询
     * @param assigned
     * @return
     */
    @GetMapping("/list")
    public R list(AssignedEntity assigned) {
        log.info("------------AssignedController | list | input : {} ", JSON.toJSONString(assigned));
        IPage<AssignedEntity> page = assignedService.selectPage(assigned);
        log.info("------------AssignedController | list | return : {} ", JSON.toJSONString(page));
        return R.ok(page);
    }

    /**
     * 添加排课
     * @param assigned
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody AssignedEntity assigned) {
        log.info("---------AssignedController | add | input : {}", JSON.toJSON(assigned));
        int row = assignedService.add(assigned);
        if (row <= 0) {
            return R.fail("添加失败");
        }
        return R.ok("添加成功");
    }

    /**
     * 批量添加排课
     * @param assignedVo
     * @return
     */
    @PostMapping("/addList")
    public R addList(@RequestBody AssignedVo assignedVo) {
        log.info("---------AssignedController | add | input : {}", JSON.toJSON(assignedVo));
        Map<String, List<Long>> map = assignedVo.getMap();
        List<Long> studentIds = map.get("studentIds");
        List<Long> courseIds = map.get("courseIds");
        AssignedEntity assigned = new AssignedEntity();
        assigned.setCreateBy(assignedVo.getUsername());
        for (Long studentId : studentIds){
            for (Long courseId : courseIds){
                assigned.setId(null);
                assigned.setStudentId(studentId);
                assigned.setCourseId(courseId);
                if (assignedService.add(assigned) <= 0) {
                    return R.fail("添加失败");
                }
            }
        }
        return R.ok("添加成功");
    }

    /**
     * 修改
     * @param assigned
     * @return
     */
    @PutMapping("/update")
    public R update(@RequestBody AssignedEntity assigned) {
        log.info("---------AssignedController | update | input : {}", JSON.toJSON(assigned));
        if (null == assigned.getId() || assigned.getId() <= 0) {
            return R.fail("参数错误:ID未传入");
        }
        int row = assignedService.update(assigned);
        if (row <= 0) {
            return R.fail("修改失败");
        }
        return R.ok("修改成功");
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") Long[] ids) {
        log.info("---------AssignedController | delete | input : {}", JSON.toJSON(ids));
        int row = assignedService.deleteBatch(ids);
        if (row <= 0) {
            return R.fail("删除失败");
        }
        return R.ok("删除成功");
    }

    /**
     * 根据id查询排课数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getInfo(@PathVariable("id") Long id) {
        log.info("---------AssignedController | getInfo | input : {}", JSON.toJSON(id));
        return R.ok(assignedService.getOne(id));
    }
}
